import { createRouter, createWebHashHistory } from 'vue-router'
import tipUtil from "@/utils/tipUtil";

const routes = [
  //-----------------------------------------------商品卡片视图-----------------------------------------------
  {
    path: '/',
    name: 'FarmCropsHome',
    component: () => import('@/views/farm-crops/FarmCropsHome'),
  },
  {
    path: '/farmCropsList',
    name: 'FarmCropsList',
    component: () => import('@/views/farm-crops/FarmCropsList'),
  },
  //-----------------------------------------------商品详情视图-----------------------------------------------
  {
    path: '/farmCropsDetails',
    name: 'FarmCropsDetails',
    meta: {
      login_require: true,
    },
    component: () => import('@/views/farm-crops/FarmCropsDetails'),
    children: []
  },
  //-----------------------------------------------用户操纵面板-----------------------------------------------
  {
    path: '/user/userInfo',
    name: 'UserInfo',
    meta: {
      login_require: true,
      user: true,
    },
    component: () => import('@/views/user/UserInfo'),
  },
  {
    path: '/user/userAddress',
    name: 'UserAddress',
    meta: {
      login_require: true,
      user: true,
    },
    component: () => import('@/views/user/UserAddress'),
  },
  {
    path: '/book/shopCar',
    name: 'ShopCar',
    meta: {
      login_require: true,
      user: true,
    },
    component: () => import('@/views/book/ShopCar'),
  },
  {
    path: '/book/bookList',
    name: 'BookList',
    meta: {
      login_require: true,
      user: true,
    },
    component: () => import('@/views/book/BookList'),
  },
  {
    path: '/book/bookOrigin',
    name: 'BookOrigin',
    meta: {
      login_require: true,
      user: true,
    },
    component: () => import('@/views/book/BookOrigin'),
  },
  {
    path: '/payFarmCrops/payFarmCropsList',
    name: 'PayFarmCropsList',
    meta: {
      login_require: true,
      user: true,
    },
    component: () => import('@/views/pay-farm-crops/PayFarmCropsList'),
  },
  //-----------------------------------------------商户操纵面板-----------------------------------------------
  {
    path: '/user/merchantInfo',
    name: 'MerchantInfo',
    meta: {
      login_require: true,
      merchant: true,
    },
    component: () => import('@/views/user/MerchantInfo'),
  },
  {
    path: '/merchant/addProduce',
    name: 'AddProduce',
    meta: {
      login_require: true,
      merchant: true,
    },
    component: () => import('@/views/merchant/AddProduce'),
  },
  {
    path: '/merchant/manageProduce',
    name: 'ManageProduce',
    meta: {
      login_require: true,
      merchant: true,
    },
    component: () => import('@/views/merchant/ManageProduce'),
  },
  {
    path: '/merchant/updateProduce',
    name: 'UpdateProduce',
    meta: {
      login_require: true,
      merchant: true,
    },
    component: () => import('@/views/merchant/UpdateProduce'),
  },
  {
    path: '/merchant/shipmentProduce',
    name: 'ShipmentProduce',
    meta: {
      login_require: true,
      merchant: true,
    },
    component: () => import('@/views/merchant/ShipmentProduce'),
  },
  //-----------------------------------------------共用账户注销面板-----------------------------------------------
  {
    path: '/account/AccountRemoval',
    name: 'AccountRemoval',
    meta: {
      login_require: true,
      merchant: false,
    },
    component: () => import('@/views/user/AccountRemoval'),
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  if (to.matched.some(function (item) {
    return item.meta.login_require && localStorage.getItem("token") === null
  })) {
    tipUtil.errTip("您还未登录！请先登录！");
    next('/');
  } else if (to.matched.some(function (item) {
    return item.meta.merchant && localStorage.getItem("identityCode") !== '2'
  })) {
    tipUtil.errTip("您的账号身份不是农户，无权访问该页面！");
    next('/');
  } else if (to.matched.some(function (item) {
    return item.meta.user && localStorage.getItem("identityCode") !== '1'
  })) {
    tipUtil.errTip("您的账号身份不是普通用户，无权访问该页面！");
    next('/');
  } else {
    next();
  }
})

export default router
